package com.imooc.api.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * Created with IntelliJ IDEA.
 *
 * @auther Levi
 * @Date: 2021/03/21/13:42
 * @Description:
 */
@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_ARTICLE = "exchange_article";
    public static final String QUEUE_DOWNLOAD_HTML = "queue_download_html";

    /**
     * 创建Exchange
     */
    @Bean(EXCHANGE_ARTICLE)
    public Exchange exchange() {
        return ExchangeBuilder
                .topicExchange(EXCHANGE_ARTICLE)//创建交换机，使用topic类型，并且定义名称
                .durable(true)//持久化，重启MQ之后仍然存在
                .build();//build创建
    }

    /**
     * 创建queue
     * @return
     */
    @Bean(QUEUE_DOWNLOAD_HTML)
    public Queue queue() {
        return new Queue(QUEUE_DOWNLOAD_HTML);//创建Queue
    }

    /**
     * 把Queue   绑定到   Exchange
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding binding(
            @Qualifier(QUEUE_DOWNLOAD_HTML) Queue queue,
            @Qualifier(EXCHANGE_ARTICLE) Exchange exchange) {
        return BindingBuilder//定义绑定关系
                .bind(queue)//绑定queue
                .to(exchange)//绑定到exchange
                .with("article.#.do")//定义路由规则
                .noargs();//执行绑定
    }


}
